define (require, exports, module) ->
  # 加载样式
  require 'mods/pay/pay_one/pay_one_css.css'
  # 引入api服务
  pay_one_api = require 'mods/pay/pay_one/pay_one_api'
  # 引入auth接口，获取用户信息
  {getUserSession} = require 'mods/auth/auth_api'

  # 定义组件
  module.exports = Vue.extend

    # 组件模板
    template: require 'mods/pay/pay_one/pay_one_tpl.js'

    # 定义变量
    data: () ->
      # 是否加载完成
      loadedSucc: false
      # 加载失败提示语
      loadedFail: ''
      # 在线支付产品信息
      buyProdInfo: {}
      # 是否已开通客户编号
      openClientFlg: false
      # 用户名
      userName: ''
      # 开客户号提交的参数
      openClientOpt:{}

    # 定义方法
    methods:

      # 插件加载
      pluginLoad: () ->
        self = @

      # 加载成功
      loadedSuccFunc: () ->
        self = @
        self.$nextTick () ->
          # 加载成功标识
          self.loadedSucc = true
          # 插件加载
          self.pluginLoad()

      # 查看详情
      goDetail: (_id) ->
        self = @
        self.$router.push '/product/' + _id

      # 在线支付步骤1提交
      onSubmit1: () ->
        self = @
        self.$router.push {name:'pay_online', params: self.buyProdInfo}

      # 获取用户登录信息
      getUserSession: () ->
        self = @
        return getUserSession()
          .then (res) ->
            if 200 isnt res?.status
              self.logined = false
            else
              self.userName = res?.body?.name or res?.body?.phone
              self.logined = true
          .catch (err) ->
            console.log err

      # 修正日期类型 1990-01-01 为 数字19900101
      fixDate: (dateStr) ->
        tmp_year = dateStr?.substring 0, 4
        tmp_month = dateStr?.substring 5, dateStr?.length - 3
        tmp_day = dateStr?.substring dateStr?.length - 2
        tmpDate = tmp_year + tmp_month + tmp_day
        return parseInt(tmpDate)

      # 开通客户号
      openClientFun: () ->
        self = @

        # 默认参数
        openParams =
          "op_branch_no": 8888,
          "op_station": "",
          "op_entrust_way": "7",
          "branch_no": 8888,
          "prodta_no": "87",
          "nationality": "CHN",
          "dsorgan_flag": "0",
          "action_in": 1,
          "mobile_tel": "",
          "contact_name": "",
          "contact_id_kind": "",
          "contact_id_no": "",
          "agency_no": "GF9"

        # 日期格式化
        self.openClientOpt.id_begindate = self.fixDate(self.openClientOpt.id_begindate)
        self.openClientOpt.id_enddate = self.fixDate(self.openClientOpt.id_enddate)
        
        # 合并填写参数
        openParams = _.merge(openParams, self.openClientOpt)

        return pay_one_api.openClient openParams

          .then (res) ->
            tmp = res
          .catch (err) ->
            console.error 'err:', err

      # setInitData: () ->
      #   self = @
      #   self.openClientOpt.id_begindate = new Date('1990-01-01')

    # 创建时触发
    created: () ->
      self = @
      self.buyProdInfo = self.$route.params

    # 计算属性
    computed:

      # 加载中的样式
      loaddingClass: () ->
        self = @
        if self.loadedSucc
          return 'c-hidden'
        if self.loadedFail
          return 'c-hidden'
        return ''

      # 加载成功的样式
      loadedSuccClass: () ->
        self = @
        return if self.loadedSucc then '' else 'c-vhidden'

      # 加载失败的样式
      loadedFailClass: () ->
        self = @
        return if self.loadedFail then '' else 'c-hidden'

    # 引入组件
    components:
      'c-pay-step': require 'mods/pay/pay_step/pay_step_mod'

    # 组件初始化
    mounted: () ->
      self = @
      self.getUserSession()
      # 加载成功 - 处理函数
      return self.loadedSuccFunc()